package com.example.demo4;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "loginServlet",value = "/loginServlet")
public class LoginServlet extends HttpServlet {
    private String username = "root";
    private String password = "123";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if(this.username.equals(username) && this.password.equals(password)){
            req.getSession().setAttribute("USER_SESSION",req.getSession().getId());

            resp.sendRedirect(this.getServletContext().getContextPath() + "/sys/Home.jsp");
        }else {
            resp.sendRedirect(this.getServletContext().getContextPath() + "/Login.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object userSession = req.getSession().getAttribute("USER_SESSION");
        if(userSession != null){
            req.getSession().removeAttribute("USER_SESSION");
            resp.sendRedirect(this.getServletContext().getContextPath() + "/Login.jsp");
        }
    }
}
